from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy  # 导入扩展类
from sqlalchemy.orm import DeclarativeBase

class Base(DeclarativeBase):
  pass

db = SQLAlchemy(model_class=Base)  # 初始化扩展，传入程序实例 app
login_manager = LoginManager()  # 实例化扩展类

login_manager.login_view = 'login'

@login_manager.user_loader
def load_user(user_id):  # 创建用户加载回调函数，接受用户 ID 作为参数
    from watchlist.models import User
    user = db.session.get(User, int(user_id))  # 用 ID 作为 User 模型的主键查询对应的用户
    return user  # 返回用户对象